using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Windows;

using VirtualDrum.componets;
using VirtualDrum.lib;
using VirtualDrum.lib.db;
using VirtualDrum.util;


namespace VirtualDrum
{
    public partial class frmMain : Form
    {

		private static ControlDatabase DBServer = new DBFactory().createDBServer();
		private DBFactory objDBF = new DBFactory();

		/// <summary>
		/// 构造函数
		/// </summary>
        public frmMain(string filePath)
        {
            InitializeComponent();
			this.hiddenDrum();
			this.openFileByClickArgs(filePath);
        }

		/// <summary>
		/// 通过程序变量的方式来打开表演文件
		/// </summary>
		/// <param name="filePath"></param>
		private void openFileByClickArgs(string filePath)
		{
			if(null == filePath) return;
			if(false == LittleTools.checkFileExists(filePath)) return ;
			this.doLoadDrumShowFile(filePath);
		}

		/// <summary>
		/// 窗体加载时的动作
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Activate();
			this._perRowDrums = 3;
			this._hashBeatDrumInfo = null;
			this._curDrumGroupName = null;
			this._curDrumGroupMainMusic = null; 
			this._hashBeatDrumInfo = new Hashtable();
			this.initLBDrumGroup();
        }

		/// <summary>
		/// 初始化各个种鼓
		/// </summary>
		private void initDrum(IList<string[]> listDrumGroupMusic)
		{
			int i = 0;
			int marginTop = 0;
			int[] arrDrumSizeInfo = new int[2] { 141, 102 };
			int[] arrLocationInfo = new int[2] { 17, 20 };
			int[] arrMarginInfo = new int[2] { 152, 103 };
			this.initArrayDrumPlayer(listDrumGroupMusic.Count);
			IEnumerator ieResult = listDrumGroupMusic.GetEnumerator();
			while (ieResult.MoveNext())
			{
				object[] arrStr = (object[])ieResult.Current;
				int x = arrLocationInfo[0];
				int y = arrLocationInfo[1];
				if (i != 0 && (i % this._perRowDrums == 0))
				{
					marginTop += arrMarginInfo[1];
				}
				else
				{
					x += arrMarginInfo[0] * (i % this._perRowDrums);
				}
				y += marginTop;
				//System.Console.WriteLine("x = " + x + ",y = " + y);
				this._arrObjDrum[i] = new Button();
				this._arrObjDrum[i].Name = "btnDrum_" + i;
				this._arrObjDrum[i].Text = "鼓音[" + i + "]:" + arrStr[0];
				this._arrObjDrum[i].TabIndex = 9;
				this._arrObjDrum[i].Location = new System.Drawing.Point(x, y);
				this._arrObjDrum[i].Size = new System.Drawing.Size(arrDrumSizeInfo[0], arrDrumSizeInfo[1]);
				this._arrObjDrum[i].UseVisualStyleBackColor = true;
				this._arrObjDrum[i].BackColor = System.Drawing.Color.Silver;
				this._arrObjDrum[i].KeyUp += new System.Windows.Forms.KeyEventHandler(this.beatDrumKeyUp);
				this._arrObjDrum[i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.beatDrumMouseDown);
				this._arrObjDrum[i].MouseUp += new System.Windows.Forms.MouseEventHandler(this.beatDrumMouseUp);
				this._arrObjDrum[i].KeyDown += new System.Windows.Forms.KeyEventHandler(this.beatDrumKeyDown);
				this.gbDrumMusic.Controls.Add(this._arrObjDrum[i]);
				if (false == this.initAxWMPPlayer(i, arrStr[1].ToString()))
				{
					this._arrObjDrum[i].Text = "鼓音文件找不到";
					this._arrObjDrum[i].Enabled = false;
				}
				i ++;
			}
		}

		/// <summary>
		/// 隐藏界面原有的Button按键
		/// </summary>
		private void hiddenDrum(){
			this.btnDrum_7.Visible = false;
			this.btnDrum_8.Visible = false;
			this.btnDrum_9.Visible = false;
		}

		/// <summary>
		/// 初始化与鼓对应的播放器
		/// </summary>
		private Boolean initAxWMPPlayer(int i,string musicPath)
		{
			if (false == LittleTools.checkFileExists(musicPath))
			{
				LittleTools.showMsg("没有找到对应的文件:" + musicPath,frmMain.projectName);
				return false;
			}
			this._arrObjPlayer[i] = new VirtualDrum.lib.Player(this._resources, "player" + i);
			((System.ComponentModel.ISupportInitialize)(this._arrObjPlayer[i].ObjPlayer)).BeginInit();
			this._arrObjPlayer[i].MusicPath = musicPath;
			this.Controls.Add(this._arrObjPlayer[i].ObjPlayer);
			((System.ComponentModel.ISupportInitialize)(this._arrObjPlayer[i].ObjPlayer)).EndInit();
			return true;
		}

		/// <summary>
		/// 当键按下时改变事件对象的背景颜色到按下状态
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void beatDrumKeyDown(object sender, KeyEventArgs e)
		{
			this.execute(this.getDrumNum(e), Color.DodgerBlue);
		}

		/// <summary>
		/// 当键弹起改变事件对象的背景色到正常值
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void beatDrumKeyUp(object sender, KeyEventArgs e)
		{
			if (false == this.checkDrumNumExists(this.getDrumNum(e))) return;
			this.changeDrumBackgroundColor(this.getCurDrum(this.getDrumNum(e)), Color.Silver);
		}

		/// <summary>
		/// 当鼠标按下改变事件对象背景色为打击状态
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void beatDrumMouseDown(object sender, MouseEventArgs e)
		{
			this.execute(this.getDrumNum(sender), Color.DodgerBlue);
		}

		/// <summary>
		/// 当鼠标松开改变事件对象背景色为正常状态
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void beatDrumMouseUp(object sender,MouseEventArgs e)
		{
			if (false == this.checkDrumNumExists(this.getDrumNum(sender))) return;
			this.changeDrumBackgroundColor(this.getCurDrum(this.getDrumNum(sender)), Color.Silver);
		}

		/// <summary>
		/// 改变对象的背景色
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="color"></param>
		private void changeDrumBackgroundColor(object sender,Color color){
			Button btnDrum	= (Button)sender;
			btnDrum.Focus();
			btnDrum.BackColor = color;
		}

		/// <summary>
		/// 执行打击鼓的相关动作：
		/// 1.设置当前的音源路径，并播放鼓音；
		/// 2.改变当前的鼓键颜色。
		/// </summary>
		/// <param name="drumNum"></param>
		/// <param name="color"></param>
		private void execute(int drumNum, Color color)
		{
			if (false == this.checkDrumNumExists(drumNum)) return;
			this._arrObjPlayer[drumNum].stop();
			this._arrObjPlayer[drumNum].play();
			this.changeDrumBackgroundColor(this.getCurDrum(drumNum), color);
			this.recordBeatDrumInfo(drumNum);
		}

		/// <summary>
		/// 表演文件的根结点
		/// </summary>
		private static readonly string rootNodeName = "virturldrum";

		/// <summary>
		/// 打击时钟(单位毫秒)
		/// </summary>
		private int _playDrumClock;

		/// <summary>
		/// 打击鼓的信息数组
		/// </summary>
		private IList<string[,]> _ilBeatDrumInfo = new List <string [,]>();

		/// <summary>
		/// 记录当前打击鼓的信息
		/// </summary>
		/// <param name="drumNum">当前鼓的编号</param>
		private void recordBeatDrumInfo(int drumNum)
		{
			//当还没有开启玩鼓记时器时不对当前的打击作保存
			if(this.timerPlayDrumClock.Enabled == false) return;
			this._ilBeatDrumInfo.Add(this.getBeatDrumNodeArray(drumNum));
		}

		/// <summary>
		/// 得到打击鼓的数组信息
		/// </summary>
		/// <param name="drumNum"></param>
		/// <returns></returns>
		private string[,] getBeatDrumNodeArray(int drumNum)
		{
			return new string[,] {{ "number", drumNum.ToString() }, 
								  { "time", this._playDrumClock.ToString() } 
								 };
		}

		/// <summary>
		/// 初始化鼓组信息从XML文件
		/// </summary>
		/// <param name="filePath"></param>
		public void initCurDrumGroupInfoFromXMLFile(string filePath){
			FileOperation objFO = new FileOperation(filePath);
			XML objXML = XML.getInstance();
			objXML.loadXMLCotent(objFO.getFileContentByString());
			objXML.RootNodeName = frmMain.rootNodeName;
			//成功拿到了都！！！^_^	
			this.initCurGoupInfoFromXMLFile(objXML);
			this.initCurDrumGroupMusicFromXMLFile(objXML);
			this.getShowTimeAndDrumNumberFromXMLFile(objXML);
		}

		/// <summary>
		/// 从XML文件里得到当前的鼓组信息
		/// </summary>
		/// <param name="objXML"></param>
		private void initCurGoupInfoFromXMLFile(XML objXML)
		{
			IList<string[]> listDrumGroupInfo = objXML.readXMLElementAttr(frmMain.rootNodeName + "/drumgroupinfo",
															new string[] { "name", "mainmusic", "foothatmusic", "foothatrate" });
			foreach (string[] arrStr in listDrumGroupInfo)
			{
				this._curDrumGroupName = arrStr[0] ?? null;
				this._curDrumGroupMainMusic = arrStr[1];
				//this.lbDrumGroup.Items.Add("xvd-" + arrStr[0] + ":" + arrStr[1]);
				frmMain.footHatsMusicPath = arrStr[2];
				frmMain.footHatsTimerRate = arrStr[3] == "" ? 500 : Int32.Parse(arrStr[3]);
				this.initAxWMPMusic();
				break;
			}
		}

		/// <summary>
		/// 从XML文件里加载鼓组音源
		/// </summary>
		/// <param name="objXML"></param>
		private void initCurDrumGroupMusicFromXMLFile(XML objXML)
		{
			IList<string[]>  listDrumGroupMusic = objXML.readXMLAllElementInfo(frmMain.rootNodeName + "/musics",
																			   new string[] { "name" },
																			   new string[] { "music" });
			IList<string[]> listDrumMusicFromXML = new List<string[]>();
			for (int i = 0; i < listDrumGroupMusic.Count; i++)
			{
				listDrumMusicFromXML.Add(new string[] { listDrumGroupMusic[i][0], listDrumGroupMusic[++i][0] });
			}
			this.initDrum(listDrumMusicFromXML);
		}

		/// <summary>
		/// 表演鼓组的打击信息哈希表
		/// </summary>
		Hashtable _hashBeatDrumInfo;
		/// <summary>
		/// 从XML文件里得到当前的打击演凑信息
		/// </summary>
		/// <param name="objXML"></param>
		private void getShowTimeAndDrumNumberFromXMLFile(XML objXML)
		{

			IList<string[]> listTimeAndDrumNum = objXML.readXMLAllElementInfo(frmMain.rootNodeName + "/beatdrums",
																new string[] { "number", "time" },
																null);			
			foreach (string[] arrStr in listTimeAndDrumNum)
			{
				if(this._hashBeatDrumInfo.Contains(arrStr[1])){
					arrStr[1] = (Int32.Parse(arrStr[1]) + 1).ToString();
				}
				this._hashBeatDrumInfo.Add(arrStr[1], Int32.Parse(arrStr[0]));
			}
		}

		/// <summary>
		/// 将当前鼓组信息写入XML文件中
		/// </summary>
		/// <param name="objXML"></param>
		/// <returns></returns>
		private Boolean writeCurGroupInfoToXMLFile(XML objXML)
		{
			return objXML.writeXMLElement("drumgroupinfo", this.getCurDrumGroupToArray());
		}

		/// <summary>
		/// 得到当前鼓组的二组信息数组 
		/// </summary>
		/// <returns></returns>
		private string [][,] getCurDrumGroupToArray(){
			string[][,] arrTempStr = new string[1][,];
			arrTempStr[0] = new string[,] { { "name", this._curDrumGroupName }, 
											{ "mainmusic", this._curDrumGroupMainMusic },
											{ "foothatmusic", frmMain.footHatsMusicPath},
											{ "foothatrate", frmMain.footHatsTimerRate.ToString()}
										  };

			return arrTempStr;
		}
		
		/// <summary>
		/// 将当前组的音源信息写入到XML文件中
		/// </summary>
		/// <returns></returns>
		private Boolean writeCurGroupMusicToXMLFile(XML objXML){
			return objXML.writeXMLElement("musics",this.getCurDrumGroupMusicPathToArray(),this.getCurDrumGroupMusicNameToArray());
		}

		/// <summary>
		/// 得到当前鼓组的音源名称到二维数组
		/// </summary>
		/// <returns></returns>
		private string [][] getCurDrumGroupMusicNameToArray(){
			string[][] arrDrumGroupMusicNode = new string[this._arrObjPlayer.Length][]; 
			for (int i = 0; i < this._arrObjDrum.Length; i++)
			{
				arrDrumGroupMusicNode[i] = new string []{ "music", this._arrObjPlayer[i].MusicPath };
			}

			return arrDrumGroupMusicNode;
		}

		/// <summary>
		/// 得到当前鼓组的音源路径到二维数组
		/// </summary>
		/// <returns></returns>
		private string[][,] getCurDrumGroupMusicPathToArray()
		{
			string[][,] arrDrumGroupMusicAttr = new string[this._arrObjDrum.Length][,];
			for (int i = 0; i < this._arrObjDrum.Length; i++)
			{
				arrDrumGroupMusicAttr[i] = new string[,] { { "name", this.getDrumName(i) } };
			}

			return arrDrumGroupMusicAttr;
		}

		/// <summary>
		/// 得到鼓音名
		/// </summary>
		/// <param name="drumNum"></param>
		/// <returns></returns>
		private string getDrumName(int drumNum){
			return this._arrObjDrum[drumNum].Text.Substring(this._arrObjDrum[drumNum].Text.IndexOf(':') + 1);
		}

		/// <summary>
		/// 写入记录到XML文件中
		/// </summary>
		private void writeRecordToXMLFile(string filePath){
			string[] arrTagName = new string[] { "",""};
			XML objXML = XML.getInstance();
			objXML.RootNodeName = frmMain.rootNodeName;
			objXML.createXMLFile(filePath);
			if (false == LittleTools.checkFileExists(filePath))
			{
				this.showMsg("创建文件失败！");
			}
			objXML.loadXMLFile(filePath);
			this.writeCurGroupInfoToXMLFile(objXML);
			this.writeCurGroupMusicToXMLFile(objXML);
			XML.tempXmlElement = objXML.createElement(frmMain.rootNodeName, "beatdrums");
			IEnumerator objE = this._ilBeatDrumInfo.GetEnumerator();
			while(objE.MoveNext()){
				XML.tempXmlElement	= objXML.writeXMLElementJustAttr(XML.tempXmlElement, "beatdrum", (string[,])objE.Current);
			}
			FileOperation objFO = new FileOperation(filePath);
			objFO.writeContentToFile(objXML.ObjXMLD.InnerXml);			
			this.showMsg("保存成功！");
			objXML.ObjXMLD.RemoveAll();
		}

		/// <summary>
		/// 检测输入鼓值是否存在
		/// </summary>
		/// <param name="drumNum"></param>
		/// <returns></returns>
		private Boolean checkDrumNumExists(int drumNum)
		{
			if (drumNum < 0 || drumNum >= this._arrObjPlayer.Length)
			{
				return false;
			}			

			return true;
		}

		/// <summary>
		/// 得到对象的下标值通过对象的名称
		/// </summary>
		/// <param name="sender"></param>
		private int getDrumNum(object sender){
			Button objDrum = (Button)sender;
			string [] arrName = objDrum.Name.Split('_');

			return Int32.Parse(arrName[1]);
		}

		/// <summary>
		/// 得到对象的下标值通过键盘输入
		/// </summary>
		/// <param name="e"></param>
		/// <returns></returns>
		private int getDrumNum(KeyEventArgs e)
		{
			return (int)e.KeyCode - 96;
		}

		/// <summary>
		/// 显示当前按键的数值
		/// </summary>
		/// <param name="e"></param>
		private Button getCurDrum(int drumNumber)
		{
			if(this._arrObjDrum[drumNumber] == null ){
				MessageBox.Show("对应鼓没有初始化" + drumNumber);
			}			

			return this._arrObjDrum[drumNumber];;
		}			

		/// <summary>
		/// 程序关闭
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		/// <summary>
		/// 当前所有的鼓音按钮
		/// </summary>
		private System.Windows.Forms.Button[] _arrObjDrum;

		/// <summary>
		/// 当前鼓对应的的播放器
		/// </summary>
		private VirtualDrum.lib.Player[] _arrObjPlayer;

		/// <summary>
		/// 每一行多少个鼓的配置
		/// </summary>
		private int _perRowDrums;

		/// <summary>
		/// 当前鼓组的名称
		/// </summary>
		public static string curDrumGroupName = null;

		/// <summary>
		/// 点击加载鼓组事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnLoadDrumGroup_Click(object sender, EventArgs e)
		{
			if(false == this.checkCurDrumGroup()) return;
			frmMain.footHatsMusicPath = "";
			this._hashBeatDrumInfo.Clear();
			this.loadDrumGroup();
			this._curDrumGroupMainMusic = this.getCurDrumGroupMainMusic();
			this.initAxWMPMusic();
			this.btnStartPlayDrum.Enabled = true;
			if (DialogResult.OK == this.showMsg("加载成功,请点击“开始玩鼓”。要是需要配置节拍，请点击“确定”。", frmMain.projectName, MessageBoxButtons.OKCancel))
			{
				this.showFootHatConfigForm();
			}
		}

		/// <summary>
		/// 显示带有回传按钮参数的提示框
		/// </summary>
		/// <param name="msg"></param>
		/// <param name="title"></param>
		/// <param name="msgBottons"></param>
		/// <returns></returns>
		private DialogResult showMsg(string msg,string title,MessageBoxButtons msgBottons){
			return MessageBox.Show(msg, title, msgBottons);
		}

		/// <summary>
		/// 检查当前鼓组是否为空或非法
		/// </summary>
		/// <returns></returns>
		private Boolean checkCurDrumGroup()
		{
			if (this.lbDrumGroup.SelectedIndex < 0)
			{
				MessageBox.Show("请选择鼓组！");
				return false;
			}

			return true;
		}

		/// <summary>
		/// 得到当前的打开鼓组信息串
		/// </summary>
		/// <returns></returns>
		private string getCurDrumGroupItemString(){
			return this.lbDrumGroup.Items[this.lbDrumGroup.SelectedIndex].ToString();
		}

		/// <summary>
		/// 得到当前打开鼓组名称
		/// </summary>
		/// <returns></returns>
		private string getCurDrumGroupName(){
			return this.getCurDrumGroupItemString().Substring(0, this.getCurDrumGroupItemString().IndexOf(':'));
		}

		/// <summary>
		/// 得到当前打开鼓组的主音乐路径
		/// </summary>
		/// <returns></returns>
		private string getCurDrumGroupMainMusic()
		{
			if (this.getCurDrumGroupItemString().IndexOf(':') == this.getCurDrumGroupItemString().Length) return null;
			string tmpStr = this.getCurDrumGroupItemString().Substring(this.getCurDrumGroupItemString().IndexOf(':') + 1);
			return tmpStr == "无" ? null : tmpStr;
		}

		/// <summary>
		/// 检测当前鼓组是否含有主音乐文件
		/// </summary>
		/// <returns></returns>
		private Boolean checkDrumGroupMainMusic()
		{
			if (this._curDrumGroupMainMusic == null)
			{
				return false;
			}

			return true;
		}
				
		/// <summary>
		/// 得到当前的鼓组音源
		/// </summary>
		/// <returns></returns>
		private IList<string[]> getCurDrumGroupMusic()
		{
			string sql = string.Format("SELECT [musicName],[musicPath],[musicGroupName] from [vd_drummusic] WHERE [musicGroupName] = '{0}' ORDER BY [musicID];", this.getCurDrumGroupName());

			return DBServer.executeQuery(sql);
		}

		/// <summary>
		/// 加载鼓组
		/// </summary>
		private void loadDrumGroup(){
			this.gbDrumMusic.Controls.Clear();
			this._curDrumGroupName = this.getCurDrumGroupName();
			this._curDrumGroupMainMusic = this.getCurDrumGroupMainMusic();
			this.initDrum(this.getCurDrumGroupMusic());
		}

		/// <summary>
		/// 初始化主音乐播放器
		/// </summary>
		private void initAxWMPMusic(){
			if (this._curDrumGroupMainMusic == null) return;
			this.axWMPMusic.URL = this._curDrumGroupMainMusic;
			this.axWMPMusic.Ctlcontrols.stop();
		}

		/// <summary>
		/// 播放主音乐
		/// </summary>
		private void startMainMusic(){
			if (true == this.checkDrumGroupMainMusic())
			{
				this.axWMPMusic.Ctlcontrols.play();
			}
		}

		/// <summary>
		/// 初始化所有的鼓播放器
		/// </summary>
		/// <returns></returns>
		private void initArrayDrumPlayer(int totalDrums)
		{
			this._arrObjDrum = new Button[totalDrums];
			this._arrObjPlayer = new VirtualDrum.lib.Player[totalDrums];
		}

		/// <summary>
		/// 刷新鼓组
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnRefDrumGroup_Click(object sender, EventArgs e)
		{
			initLBDrumGroup();
		}

		/// <summary>
		/// 初始化列表框里的鼓组信息
		/// </summary>
		private void initLBDrumGroup(){
			this.lbDrumGroup.Items.Clear();
			string sql = "SELECT [groupName],[groupMusicPath] FROM [vd_drumgroup];";
			IList<string[]> alQueryResult = DBServer.executeQuery(sql);
			IEnumerator ieResult = alQueryResult.GetEnumerator();
			while (ieResult.MoveNext())
			{
				object[] arrStr = (object [])ieResult.Current;
				this.lbDrumGroup.Items.Add(arrStr[0] + ":" + arrStr[1]);
			}
		}

		/// <summary>
		/// 停止鼓组事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnStopDrumGroup_Click(object sender, EventArgs e)
		{
			this.stopAllAxWMPPlayer();
			this.stopMainMusicAxWMP();
			this.initTimerFootHats(false);
			this.initDrumPlayClock(false);
			this.btnStopDrumPlay.Enabled = false;
		}

		/// <summary>
		/// 停止当前主音乐
		/// </summary>
		private void stopMainMusicAxWMP(){
			if(this.axWMPMusic == null) return;
			this.axWMPMusic.Ctlcontrols.stop();
		}

		/// <summary>
		/// 停止所有的鼓组播放器
		/// </summary>
		private void stopAllAxWMPPlayer(){
			for (int i = 0; i < this._arrObjPlayer.Length; i ++ )
			{
				this._arrObjPlayer[i].stop();
			}
		}

		/// <summary>
		/// 加载打开的文件到“最近打开”项里
		/// </summary>
		/// <param name="filePath"></param>
		private void addLoadFileToRecnetFile(string filePath){
			ToolStripMenuItem tmpTSMI = new ToolStripMenuItem();
			tmpTSMI.Name = "tsmi" + LittleTools.getFileName(filePath);
			tmpTSMI.Size = new System.Drawing.Size(152, 22);
			tmpTSMI.Text = filePath;
			tmpTSMI.Click += new System.EventHandler(this.openRecentFile);
			this.tsimRecentLoadFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            tmpTSMI});
		}

		/// <summary>
		/// 打开鼓组表演
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnLoadDrumShow_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK == this.ofdDrumGroupShow.ShowDialog())
			{
				doLoadDrumShowFile(this.ofdDrumGroupShow.FileName);
			}
		}

		/// <summary>
		/// 执行加载鼓组文件动作
		/// </summary>
		/// <param name="filePath"></param>
		private void doLoadDrumShowFile(string filePath){
			if (false == LittleTools.checkFileExists(filePath))
			{
				LittleTools.showMsg("文件不存在，请确认！", frmMain.projectName);
				return;
			}
			this.gbDrumMusic.Controls.Clear();
			this._hashBeatDrumInfo.Clear();
			this.ofdDrumGroupShow.InitialDirectory = LittleTools.getAppCurDirectory();
			this.initCurDrumGroupInfoFromXMLFile(filePath);
			this.btnStartPlayDrum.Enabled = true;
			this.addLoadFileToRecnetFile(filePath);
			DialogResult result = this.showMsg("加载成功,请点击“开始玩鼓”。要是需要配置节拍，请点击“确定”。",
									  frmMain.projectName,
									  MessageBoxButtons.OKCancel);
			if (DialogResult.OK == result)
			{
				this.showFootHatConfigForm();
			}
		}

		/// <summary>
		/// 开始玩鼓
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnRecodeDrumShow_Click(object sender, EventArgs e)
		{
			if (this._arrObjDrum[0] != null ) this._arrObjDrum[0].Focus();
			this.initTimerFootHats(true);
			this.initDrumPlayClock(true);
			this.startMainMusic();
			this.btnStopDrumPlay.Enabled = true;
		}

		/// <summary>
		/// 显示默认提示信息
		/// </summary>
		private void showMsg()
		{
			MessageBox.Show("正在添加中，敬请期待....",projectName);
		}

		/// <summary>
		/// 显示给以参数的提示信息
		/// </summary>
		/// <param name="msg"></param>
		private void showMsg(string msg){
			MessageBox.Show(msg,projectName);
		}

		/// <summary>
		/// 帮助窗体显示
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void stmiChm_Click(object sender, EventArgs e)
		{
			this.showMsg();
		}

		/// <summary>
		/// 显示关于作者
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsmiAbutuAuthor_Click(object sender, EventArgs e)
		{
			this.showMsg("网名：九九\n爱好：只爱摇滚跟Coding ^_^\n个人站点：http://www.99ebc.com");
		}

		/// <summary>
		/// 显示当前版本事件 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsmiUpdateProgram_Click(object sender, EventArgs e)
		{
			this.showMsg("当前版本Version 1.0 @ CopyRight 2011.05.18");
		}

		/// <summary>
		/// 工程名
		/// </summary>
		public static readonly string projectName = "虚拟架子鼓";

		/// <summary>
		/// 当前鼓组名称
		/// </summary>
		private string _curDrumGroupName;
		/// <summary>
		/// 当前鼓组的主音乐文件
		/// </summary>
		private string _curDrumGroupMainMusic;

		/// <summary>
		/// 鼓组选项配置
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsmiDrumGroupConfig_Click(object sender, EventArgs e)
		{
			frmOptions frmOptions = new frmOptions();
			frmOptions.Show();
		}

		/// <summary>
		/// 脚镲时钟
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void timerFootHats_Tick(object sender, EventArgs e)
		{
			frmMain.objFootHatPlayer.stop();
			frmMain.objFootHatPlayer.play();
		}

		/// <summary>
		/// 初始化节拍播放器
		/// </summary>
		/// <param name="enableFlag"></param>
		private void initTimerFootHats(Boolean enableFlag)
		{
			frmMain.objFootHatPlayer = null;
			if(!frmMain.footHatsMusicPath.Equals("")){
				this.timerFootHats.Interval = frmMain.footHatsTimerRate;
				this.timerFootHats.Enabled = enableFlag;
				if(frmMain.objFootHatPlayer == null){
					frmMain.objFootHatPlayer = new Player(this._resources, "footHatPlayer");
					((System.ComponentModel.ISupportInitialize)(frmMain.objFootHatPlayer.ObjPlayer)).BeginInit();					
					this.Controls.Add(frmMain.objFootHatPlayer.ObjPlayer);
					((System.ComponentModel.ISupportInitialize)(frmMain.objFootHatPlayer.ObjPlayer)).EndInit();
					this.Controls.Add(frmMain.objFootHatPlayer.ObjPlayer);
				}
				frmMain.objFootHatPlayer.MusicPath = frmMain.footHatsMusicPath;
				if (enableFlag == true)
				{
					this.timerFootHats.Start();
				}
				else
				{
					this.timerFootHats.Stop();
				}
				this.Controls.Add(frmMain.objFootHatPlayer.ObjPlayer);
			}
		}
		
		/// <summary>
		/// 显示脚镲配置窗口
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tmsiFootHats_Click(object sender, EventArgs e)
		{
			this.showFootHatConfigForm();
		}

		/// <summary>
		/// 显示脚镲配置窗体
		/// </summary>
		private void showFootHatConfigForm(){
			FootHatConfig frmFootHatConfig = new FootHatConfig();
			frmFootHatConfig.Show();
		}

		/// <summary>
		/// 脚镲播放器
		/// </summary>
		private static Player objFootHatPlayer = null;
		/// <summary>
		/// 脚镲音乐文件路径
		/// </summary>
		public static string footHatsMusicPath = "";
		/// <summary>
		/// 节拍率默认为2/4拍
		/// </summary>
		public static int footHatsTimerRate = 125;

		/// <summary>
		/// 击鼓的次数
		/// </summary>
		private static int beatDrumTimes	= 1;
		private static int curDrumNum = -1;
		/// <summary>
		/// 打击表演计时器
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void timerPlayDrumClock_Tick(object sender, EventArgs e)
		{
			this._playDrumClock++;
			if(this._hashBeatDrumInfo.Count > 0){
				string tmpStr = this._playDrumClock.ToString();
				if (this._hashBeatDrumInfo.Contains(tmpStr))
				{					
					this.beatDrum((int)this._hashBeatDrumInfo[tmpStr]);
					curDrumNum = (int)this._hashBeatDrumInfo[tmpStr];
					frmMain.beatDrumTimes++;
				} 
				if (curDrumNum != -1 && this._playDrumClock % 9 == 0)
				{
					this.changeDrumBackgroundColor(this.getCurDrum(curDrumNum), Color.Silver);
					if (frmMain.beatDrumTimes == this._hashBeatDrumInfo.Count - 1)
					{
						this.initDrumPlayClock(false);
					}
				}
			}			
		}

		/// <summary>
		/// 打鼓了
		/// </summary>
		/// <param name="drumNum"></param>
		private void beatDrum(int drumNum){
			this.execute(drumNum, Color.DodgerBlue);
		}

		/// <summary>
		/// 初始化鼓组打击计时器
		/// </summary>
		private void initDrumPlayClock(Boolean flag){
			this.timerPlayDrumClock.Enabled = flag;
			if (flag == false) this.timerPlayDrumClock.Stop();
			else this.timerPlayDrumClock.Start();
			this._playDrumClock = 0;
			frmMain.beatDrumTimes = 0;
		}

		/// <summary>
		/// 得到当前表演记时器的状态
		/// </summary>
		/// <returns></returns>
		private Boolean getDrumPlayClockStatus()
		{
			return this.timerPlayDrumClock.Enabled;
		}

		/// <summary>
		/// 保存当前鼓表演
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsmiSaveDrumGroupPlay_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK == this.sfdDrumGroupFile.ShowDialog())
			{
				this.writeRecordToXMLFile(this.sfdDrumGroupFile.FileName);
			}
		}

		/// <summary>
		/// 打开最近打开的鼓组文件
		/// </summary>
		private void openRecentFile(object sender, EventArgs e)
		{
			ToolStripMenuItem tmpTSIM = (ToolStripMenuItem)sender;
			this.doLoadDrumShowFile(tmpTSIM.Text);
		}

		/// <summary>
		/// 鼓组列表的鼠标右键事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void lbDrumGroup_MouseUp(object sender, MouseEventArgs e)
		{
			if(e.Button == MouseButtons.Right){
				this.lbDrumGroup.SelectedIndex = lbDrumGroup.IndexFromPoint(e.X,e.Y);
				this.cmsDrumGroup.Show(Control.MousePosition.X,Control.MousePosition.Y);
			}
		}

		/// <summary>
		/// 鼓组右键加载当前鼓组事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsmLoadDrumGroup_Click(object sender, EventArgs e)
		{
			this.btnLoadDrumGroup_Click(sender,e);
		}

		/// <summary>
		/// 将表演鼓组加入到数据库中
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsmAddShowToDB_Click(object sender, EventArgs e)
		{
			frmDrumGroupInfo frmDrumGroupInfo = new frmDrumGroupInfo();
			frmDrumGroupInfo.ActionName = "add";
			frmDrumGroupInfo.Show();
		}

		/// <summary>
		/// 右键刷新鼓组
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsmiRefreshDrumGroup_Click(object sender, EventArgs e)
		{
			this.initLBDrumGroup();
		}

		/// <summary>
		/// 右键修改当前鼓组
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsmiChangeCurDrumGroup_Click(object sender, EventArgs e)
		{
			if (false == this.checkCurDrumGroup()) return;
			frmDrumGroupInfo frmDrumGroupInfo = new frmDrumGroupInfo();
			frmDrumGroupInfo.ActionName = "change";
			frmDrumGroupInfo.GroupName = this.getCurDrumGroupName();
			frmDrumGroupInfo.GroupMusicPath = this.getCurDrumGroupMainMusic();
			frmDrumGroupInfo.Show();
		}

		/// <summary>
		/// 右键删除鼓组事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsmiDelCurGroup_Click(object sender, EventArgs e)
		{
			if (false == this.checkCurDrumGroup()) return;
			if(0 < frmOptions.delDrumGroup(this.getCurDrumGroupName())){
				this.showMsg("删除成功！");
			}else{
				this.showMsg("删除失败！");
			}
		}

		/// <summary>
		/// 清空音源事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClearDrumMusic_Click(object sender, EventArgs e)
		{
			this.initWelcomeLogo();
		}	

		/// <summary>
		/// 初始化欢迎页图
		/// </summary>
		private void initWelcomeLogo(){
			this.gbDrumMusic.Controls.Clear();
			this.gbDrumMusic.Controls.Add(this.pbLogo);
		}

    }
}
